﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using pigs;

namespace Pigs.Weapons
{
    public class Slop
    {
        // Image representing the slop
        public Texture2D Texture;

        // Position of the slop relative to the upper left side of the screen
        public Vector2 Position;

        // Position of the slop relative to the upper left side of the screen
        public Vector2 Destination;

        // Position of the slop at the start of the addslop call
        public Vector2 Start;

        // State of the slop
        public bool Active;

        //how big to draw the slop
        public float scale;

        //has the slop arrived at it's destination
        public Boolean arrived;
        
        //how far does the slop effect pigs?
        public int radius; 

        // Get the width of the slop
        public int Width
        {
            get { return Texture.Width; }
        }

        // Get the height of the slop
        public int Height
        {
            get { return Texture.Height; }
        }

        // Determines how fast the projectile moves
        float slopMoveSpeed;

        /// <summary>
        /// initialize the slop
        /// </summary>
        /// <param name="texture">texture for the slop</param>
        /// <param name="position">where the slop starts</param>
        /// <param name="direction">vector of the position it's going to</param>
        public void Initialize(Texture2D texture, Vector2 position, Vector2 destination)
        {
            this.Texture = texture;
            this.Position = position;
            this.Start = position;
            this.Destination = destination;
            this.Active = true;

            this.arrived = false;
            this.slopMoveSpeed = -.4f;
            this.radius = 150;
            this.scale = .01f;
        }

        /// <summary>
        /// update the slop and have pigs sucked into it
        /// </summary>
        /// <param name="pigs"></param>
        public void Update(List<Pig> pigs, Boolean move)
        {
            if ((arrived && scale < .01) || this.Position.X < -50)
            {
                this.Active = false;
            }
            if (arrived || Vector2.Distance(this.Position, this.Destination) < 20)
            {
                arrived = true;
                if (move)
                {
                    this.Position.X += this.slopMoveSpeed;
                }
                Vector2 slopCenter = new Vector2(this.Position.X, this.Position.Y);
                for (int i = 0; i < pigs.Count; i++)
                {
                    if (pigs[i].shouldEatSlop)
                    {
                        float distance = Vector2.Distance(slopCenter, pigs[i].position);
                        if (distance < this.radius && scale > .01)
                        {
                            pigs[i].isEatingSlop = true;
                            pigs[i].position.X += pigs[i].speed / (distance / 30);
                            pigs[i].position.Y += (slopCenter.Y - pigs[i].position.Y) / 15;
                            scale = scale - .0008f;
                            if (scale < .01)
                            {
                                pigs[i].isEatingSlop = false;
                            }
                        }
                        else
                        {
                            pigs[i].isEatingSlop = false;
                        }
                    }
                }
            }

            //move the slop closer to it's desitnation
            else
            {
                this.Position.X = Position.X + ((Destination.X - Start.X) / 40);
                this.Position.Y = Position.Y + ((Destination.Y - Start.Y) / 40);
                this.scale = this.scale + (0.9f / 40);
            }
        }

        /// <summary>
        /// draw the slop
        /// </summary>
        /// <param name="spriteBatch">spritebatch that the game uses</param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (scale > .01)
                spriteBatch.Draw(Texture, new Vector2(Position.X - (scale * Width)/2, Position.Y - (scale * Width)/2), null, Color.White, 0, new Vector2(0, 0), scale, SpriteEffects.None, 0f);
        }
    }
}
